<template>
  <div class="container">
    <aside class="sidebar">
      <div class="course-square" @click="handleCourseSquareClick"
        style="display: flex; align-items: center; justify-content: center; flex-direction: column;">
        <span>课程表</span>
      </div>
    </aside>
    <div class="right-content">
      <header class="header">
        <div class="header-left">
          <img src="@/assets/svgs/six.svg" @click="navigateTo('dashboard')" alt="Logo" class="header-icon"
            style="cursor: pointer;" />
          <div class="manager-title">AIGC智育优学教育平台</div>
        </div>
        <div class="teacher-initial" style="color: white;">{{ teacherInitial }}</div>
        <div class="user-info" @click="toggleDropdown">
          <img class="avatar" :src="userInfo?.avatarUrl" alt="User Avatar">
          <span class="username22">{{ userInfo?.nick_name }}</span>
          <img class="dropdown-arrow" src="@/assets/svgs/downnarrow.svg" alt="Dropdown Arrow">
          <div v-if="dropdownVisible" class="dropdown-menu">
            <p @click="$router.push('/teacher/information')">个人信息</p>
            <p @click="$router.push('/login')">退出登录</p>
          </div>
        </div>
      </header>
      <main class="main">
        <slot></slot>
      </main>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { useRouter } from 'vue-router';
import { informationService } from '@/api/personnal-information';
// 定义响应式数据

const userInfo = ref(null);

const fetchUserInfo = async () => {
  try {
    const result = await informationService();
    const userData = result.data; // 假设后端返回的数据结构是 { data: SysUser }
    userInfo.value = {
      userId: userData.userId,
      avatarUrl: userData.avatar,
      user_name: userData.userName,
      nick_name: userData.nickName
    };
  } catch (error) {
    console.error('获取用户信息失败:', result.message);
  }
};
fetchUserInfo();
const router = useRouter();
const dropdownVisible = ref(false);


const toggleDropdown = () => {
  dropdownVisible.value = !dropdownVisible.value;
};

const navigateTo = async (page) => {
  console.log('Navigating to:', page); // 添加调试日志

  if (page === 'manager') {
    try {
      await router.push('/student/mycourse');
      console.log('Navigation successful'); // 添加调试日志
    } catch (error) {
      console.error('Navigation error:', error); // 添加错误日志
    }
  }
  if (page === 'dashboard') {
    router.push('/student/dashboard');
  }
  if (page === 'logout') {
    router.push('/login');
  }
}

// 点击其他地方关闭下拉菜单
const closeDropdown = (e) => {
  if (!e.target.closest('.user-info')) {
    dropdownVisible.value = false;
  }
};

// 修改课程表点击事件处理
const handleCourseSquareClick = () => {
  console.log('Course square clicked'); // 添加调试日志
  navigateTo('manager');
};

onMounted(() => {
  document.addEventListener('click', closeDropdown);
});

onUnmounted(() => {
  document.removeEventListener('click', closeDropdown);
});
</script>

<style scoped>
.container {
  display: flex;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.sidebar {
  width: 60px;
  height: 100vh;
  background-color: #13227a;
  padding: 20px 10px;
  position: fixed;
  left: 0;
  top: 0;
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-top: 50px;
  z-index: 1000;
}

.course-square {
  width: 60px;
  height: 60px;
  background-color: #293a96;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  cursor: pointer;
  transition: all 0.3s;
  position: relative;
  z-index: 1001;
}

.course-square:hover,
.course-square.active {
  background-color: #213fe4;
  opacity: 1;
}

.right-content {
  flex: 1;
  margin-left: 80px;
  display: flex;
  flex-direction: column;
  background-color: white;
  z-index: 1;
}

.header {
  height: 50px;
  width: 100%;
  background-color: #13227a;
  position: fixed;
  top: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 30px;
  margin-left: 40px;
}

.manager-title {
  color: white;
  font-size: 16px;
}

h2 {
  margin-left: 0 !important;
  padding-left: 70px;
}

.main {
  margin-top: 60px;
  padding: 20px 40px;
  flex: 1;
  display: flex;
}

.user-info {
  position: relative;
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: rgba(0, 0, 0, 0.05);
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 8px;
  object-fit: cover;
}

.username {
  font-size: 14px;
  color: #ffffff;
  margin-right: 4px;
}

.dropdown-arrow {
  width: 12px;
  height: 12px;
  transition: transform 0.3s;
}

.user-info:hover .dropdown-arrow {
  transform: rotate(180deg);
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 8px;
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 4px 0;
  min-width: 120px;
  z-index: 1000;
  animation: slideDown 0.2s ease-out;
}

.dropdown-menu p {
  padding: 8px 16px;
  margin: 0;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  transition: background-color 0.3s;
}

.dropdown-menu p:hover {
  background-color: #f5f7fa;
  color: #409EFF;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>